package com.jb.workflow.app.ui;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import net.oschina.app.AppConfig;
import net.oschina.app.AppContext;
import net.oschina.app.AppException;
import net.oschina.app.R;
import net.oschina.app.bean.Post;
import net.oschina.app.bean.Result;
import net.oschina.app.common.StringUtils;
import net.oschina.app.common.UIHelper;
import net.oschina.app.ui.BaseActivity;
import net.oschina.app.ui.QuestionPub;

public class TaskPub extends BaseActivity {

	private ImageView mBack;
	private EditText mCustomer;
	private EditText mOrderId;
	private EditText mComment;
	private CheckBox mNeedDriver;
	private Button mPublish;
    private ProgressDialog mProgress;
	private Post post;
	private InputMethodManager imm;
	
	private String customer;
	private String orderId;
	private String comment;
	private String needDriver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.task_pub);
		
		this.initView();
		
	}
	
    //初始化视图控件
    private void initView()
    {    	
    	imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    	
    	mBack = (ImageView)findViewById(R.id.task_pub_back);
    	mPublish = (Button)findViewById(R.id.task_pub_publish);
    	mCustomer = (EditText)findViewById(R.id.task_pub_customer);
    	mOrderId = (EditText)findViewById(R.id.task_pub_orderid);
    	mComment = (EditText)findViewById(R.id.task_pub_comment);
    	mNeedDriver = (CheckBox)findViewById(R.id.task_need_driver);
    	
    	mBack.setOnClickListener(UIHelper.finish(this));
    	mPublish.setOnClickListener(publishClickListener);
    	//编辑器添加文本监听
    	mCustomer.addTextChangedListener(UIHelper.getTextWatcher(this, AppConfig.TEMP_TASK_CUSTOMER));
    	mOrderId.addTextChangedListener(UIHelper.getTextWatcher(this, AppConfig.TEMP_TASK_ORDERID));
    	mComment.addTextChangedListener(UIHelper.getTextWatcher(this, AppConfig.TEMP_TASK_COMMENT));
    	
    	//显示临时编辑内容
    	UIHelper.showTempEditContent(this, mCustomer, AppConfig.TEMP_TASK_CUSTOMER);
    	UIHelper.showTempEditContent(this, mOrderId, AppConfig.TEMP_TASK_ORDERID);
    	UIHelper.showTempEditContent(this, mComment, AppConfig.TEMP_TASK_COMMENT);
    }
    
	private View.OnClickListener publishClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			//隐藏软键盘
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  
			
			customer = mCustomer.getText().toString();
			if(StringUtils.isEmpty(customer)){
				UIHelper.ToastMessage(v.getContext(), "请输入客户名称");
				return;
			}
			
			orderId = mOrderId.getText().toString();
			if(StringUtils.isEmpty(orderId)){
				UIHelper.ToastMessage(v.getContext(), "请输入订单号");
				return;
			}
			comment = mComment.getText().toString();
			needDriver = mNeedDriver.isChecked() ? "true" : "false";
			
			final AppContext ac = (AppContext)getApplication();
			if(!ac.isLogin()) {
				UIHelper.showLoginDialog(TaskPub.this);
				return;
			}
			
			mProgress = ProgressDialog.show(v.getContext(), null, "发布中···", true, true); 
			
			final Handler handler = new Handler(){
				public void handleMessage(Message msg) {
					if(mProgress!=null)mProgress.dismiss();
					if(msg.what == 1){
						Result res = (Result)msg.obj;
						if(res.OK()) {
							UIHelper.ToastMessage(TaskPub.this, res.getErrorMessage());
							//发送通知广播
							if(res.getNotice() != null){
								UIHelper.sendBroadCast(TaskPub.this, res.getNotice());
							}
							//清除之前保存的编辑内容
							ac.removeProperty(AppConfig.TEMP_TASK_CUSTOMER, AppConfig.TEMP_TASK_ORDERID, AppConfig.TEMP_TASK_COMMENT);
							//跳转到文章详情
							setResult(RESULT_OK);//成功则退出并且刷新列表
							finish();
						} else { //逻辑错误，暂时不退出页面，只是提示错误信息
							UIHelper.ToastMessage(TaskPub.this, getString(R.string.wf_submit_fail) + msg.obj);
						}
					}
					else {
						((AppException)msg.obj).makeToast(TaskPub.this);
					}
				}
			};
			new Thread(){
				public void run() {
					Message msg = new Message();					
					try {
						Result res = ac.pubTask(customer, orderId, comment, needDriver);
						msg.what = 1;
						msg.obj = res;
		            } catch (AppException e) {
		            	e.printStackTrace();
						msg.what = -1;
						msg.obj = e;
		            }
					handler.sendMessage(msg);
				}
			}.start();
		}
	};
}
